package org.example.dao.imp;

import org.example.dao.inter.ActivityDao;
import org.example.entitys.managers.Activity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author 魏锴
 * @Date 2023/11/10
 */

@Repository
public class ActivityDaoImp implements ActivityDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    //创建活动
    public int insert(Activity activity) {
        String sql = "insert into " +
                "t_manager_activity(name, category, title, creator, " +
                "abstract, detailed_desc, status, created_time, pic_urls)" +
                " values (?,?,?,?,?,?,?,?,?)";
        return jdbcTemplate.update(sql, activity.getName(), activity.getCategory(),
                activity.getTitle(), activity.getCreator(), activity.getSummary(),
                activity.getDetail_desc(), 0, new Date(), activity.getPic_url());
    }

    @Override
    public int update(Activity activity) {
        String sql = "update t_manager_activity set name = ?,category=?,title=?,creator=?," +
                "abstract=?,detailed_desc=?,status=?,created_time=?,archived_time=?," +
                "pic_urls=?,start_time=?,end_time=? where id=?";
        return jdbcTemplate.update(sql, activity.getName(), activity.getCategory(),
                activity.getTitle(), activity.getCreator(), activity.getSummary(),
                activity.getDetail_desc(), activity.getStatus(), activity.getCreate_time(),
                activity.getArchived_time(), activity.getPic_url(), activity.getStart_time(),
                activity.getEnd_time(), activity.getId());

    }

    @Override
    public int delete(int id) {
        String sql = "update t_manager_activity set status = 4 where id = ? ";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public boolean isExitById(int id) {
        String sql = "select count(*) from t_manager_activity where id = ? ";
        return jdbcTemplate.queryForObject(sql, Integer.class, id) > 0;
    }

    @Override
    public Activity queryById(int id) {
        String sql = "select id,name,category,title,creator,abstract,detailed_desc," +
                "status, created_time,archived_time,pic_urls,start_time,end_time" +
                " from t_manager_activity where id = ?";
        return jdbcTemplate.queryForObject(sql, this::transform, id);
    }

    @Override
    public int queryLastInsertID() {
        String sql = "select LAST_INSERT_ID()";
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }

    @Override
    public List<Activity> queryAll() {
        String sql = "select id,name,category,title,creator,abstract,detailed_desc," +
                "status,created_time,archived_time,pic_urls,start_time,end_time " +
                "from t_manager_activity order by created_time desc";
        return jdbcTemplate.query(sql, this::transform);
    }

    @Override
    public List<Activity> querySortedByStatus(int status) {
        String sql = "select id, name, category, title, creator, abstract, " +
                "detailed_desc, status, created_time, archived_time, pic_urls, " +
                "start_time, end_time from t_manager_activity where status = ?" +
                " order by created_time desc";
        return jdbcTemplate.query(sql, this::transform, status);
    }

    @Override
    public List<Activity> queryActivityByNameAndStatus(String name, int status) {
        String sql = "select id,name,category,title,creator,abstract,detailed_desc,status," +
                "created_time,archived_time,pic_urls,start_time,end_time from t_manager_activity";
        boolean isEmpty = true;
        if (name != null && !"".equals(name)) {
            name = change(name);
            isEmpty = false;
            sql = sql + " where name = " + name;
        }
        if (status != -1) {
            if (isEmpty) sql += " where";
            else sql += " and";
            isEmpty = false;
            sql = sql + " status = " + status;
        }
        if (isEmpty) return new ArrayList<>();
        return jdbcTemplate.query(sql, this::transform);
    }

    @Override
    public Activity transform(ResultSet rs, int RowNum) throws SQLException {
        return new Activity(
                rs.getInt("id"),
                rs.getString("name"),
                rs.getString("category"),
                rs.getString("title"),
                rs.getString("creator"),
                rs.getString("abstract"),
                rs.getString("detailed_desc"),
                rs.getInt("status"),
                rs.getDate("created_time"),
                rs.getDate("archived_time"),
                rs.getString("pic_urls"),
                rs.getDate("start_time"),
                rs.getDate("end_time")
        );
    }
}